require "common"
local JCBHMapping = {"RQSPF|","JQS|","SXP|","CBF|","BQC|", "SPF|", "HH|"}
local BJDCBHMapping = {"SPF|","JQS|","SXP|","CBF|","BQC|"}
local PosMapping = { ["3*3"] = 2, ["4*6"] = 2, ["5*10"] = 2, ["6*15"] = 2, ["4*4"] = 3, ["6*20"] = 3, ["5*5"] = 4, ["7*35"] = 4, ["8*70"] = 4, ["6*6"] = 5, ["7*21"] = 5, ["7*7"] = 6, ["8*8"] = 7, ["8*28"] = 6, ["7*21"] = 5, ["8*56"] = 5}
local MaxPower = 99

function just_str(playId, butType, str)
	if butType == '1' or butType == '6' then
		if not string.find("3/1/0",str) then return nil end
	elseif butType == '2' then
		if not string.find("0/1/2/3/4/5/6/7",str) then return nil end
	elseif butType == '3' then
		if not string.find("SD/SS/XD/XS",str) then return nil end
	elseif butType == '4' then
		if playId == 'q' then
			if not string.find("1:0/2:0/3:0/4:0/2:1/3:1/4:1/3:2/4:2/9:0/0:0/1:1/2:2/3:3/9:9/0:1/0:2/0:3/0:4/1:2/1:3/1:4/2:3/2:4/0:9",str) then return nil end
		elseif playId == 'p' then
			if not string.find("1:0/2:0/3:0/4:0/5:0/2:1/3:1/4:1/5:1/3:2/4:2/5:2/9:0/0:0/1:1/2:2/3:3/9:9 /0:1/0:2/0:3/0:4/0:5/1:2/1:3/1:4/1:5/2:3/2:4/2:5/0:9",str) then return nil end
		end
	elseif butType == '5' then
		if not string.find("3-3/3-1/3-0/1-3/1-1/1-0/0-3/0-1/0-0",str) then return nil end
	end

	return true
end

function MyFloor(input)
	strinput = tostring(input)
	input = tonumber(strinput)
	return math.floor(input)
end

function SiSheLiuRuWuChengShuang(totalAmount, butPower)
	--单注奖金
	dzAward = totalAmount / butPower / 2
	strtotalAmount = tostring(dzAward)
	dzAward = tonumber(strtotalAmount)
	_,pos=string.find(strtotalAmount, '%.')
	if pos then
		--小数点第2位
		if string.sub(strtotalAmount,pos+2,pos+2) ~= '' then
			point2 = tonumber(string.sub(strtotalAmount,pos+2,pos+2))
		else
			point2 = 0
		end
		--小数点第3位
		if string.sub(strtotalAmount,pos+3,pos+3) ~= '' then
			point3 = tonumber(string.sub(strtotalAmount,pos+3,pos+3))
		else
			point3 = 0
		end
		--小数点第3位后面的余数
		if string.sub(strtotalAmount,pos+4) ~= '' then
			point3Right = tonumber(string.sub(strtotalAmount,pos+4))
		else
			point3Right = 0
		end
	else
		point2 = 0
		point3 = 0
		point3Right = 0
	end
	--四舍
	if point3 < 5 then
		dzAward = MyFloor(dzAward*100) / 100
	--六入
	elseif point3 > 5 then
		dzAward = (MyFloor(dzAward*100)  + 1) / 100
	--五成双
	else
		--当5后有数时，舍5入1
		if point3Right > 0 then
			dzAward = (MyFloor(dzAward*100)  + 1) / 100
		--5前为奇数，舍5入1
		elseif point2 % 2 == 1 then
			dzAward = (MyFloor(dzAward*100)  + 1) / 100
		--5前为偶数，舍5不进
		else
			dzAward = MyFloor(dzAward*100) / 100
		end

	end
	totalAmount = dzAward * butPower * 2
	return totalAmount
end

function Compute(pnInput, nTotal, nSelect)
	local i
	local result = 0
	local temp = {}

	if nTotal < nSelect then
		return 0
	elseif nSelect == 1 then
		for i = 1, nTotal do
			result = result + pnInput[i]
		end
	elseif nTotal == nSelect then
		result = 1
		for i = 1, nTotal do
			result = result * pnInput[i]
		end
	elseif nTotal > nSelect then
		for i = 2, #pnInput do
			temp[i - 1] = pnInput[i]
		end
		result = pnInput[1] * Compute(temp,nTotal - 1, nSelect - 1) + Compute(temp,nTotal - 1, nSelect)
	end

	return result
end



function ComputeMoney(pnInput, nTotal, nSelect)
        local i
        local result = 0
        local temp = {}
        local sp = 0
		local tempInput = {}

		for i = 1, #pnInput do
			tempInput[i] = pnInput[i]
		end

        if nSelect == 1 then
                for i = 1, nTotal do
					if pnInput[i] == -1 then
						tempInput[i] = 0
					end
					sp =sp + tempInput[i]
                end
                result =  sp * 2 * 0.65
        elseif nTotal == nSelect then
               sp = 1
                for i = 1, nTotal do
					if pnInput[i] == -1 then
						tempInput[i] = 0
					elseif pnInput[i] == 0 then
						tempInput[i] = 1
					end

					sp = sp * tempInput[i]
                end
				result = sp * 2 * 0.65
        elseif nTotal > nSelect then
                for i = 2, #pnInput do
                        temp[i - 1] = pnInput[i]
                end
				if pnInput[1] == 0 then
					tempInput[1] = 1
				elseif pnInput[1] == -1 then
					tempInput[1] = 0
				end
                result =tempInput[1] * ComputeMoney(temp,nTotal - 1, nSelect - 1) + ComputeMoney(temp,nTotal - 1, nSelect)

        end


        return result
end

function ComputeJCZQMoney(pnInput, nTotal, nSelect,vSelect, SpInput, vSpSelect)
        local i
        local result = 0
        local temp = {}
        local sp = 0
		local tempInput = {}
		local tmpresult = 0
		local vTmpSelect = {}

		local tempSpInput = {}
		local vTempSpSelect = {}
		--取消场次总注数 eg：150914001 sp=1 注码 3/1/0  150914002 sp=1 注码 3/1/0  cancel = 3*3 = 9
		cancelNum = 1

		for i = 1, #pnInput do
			tempInput[i] = pnInput[i]
		end

        if nSelect == 1 then
			for i = 1, nTotal do

				if pnInput[i] == -1 then
					tempInput[i] = 0
				end
				tmpresult = tempInput[i]
				--取消场次复式票，如果奖金大于1w，要先算单式票奖金是否大于1w，如果大于1w扣税
				if SpInput[i] == 1 then
						cancelNum = cancelNum * tempInput[i]
				end

				for j = 1, #vSelect do
					tmpresult = tmpresult * vSelect[j]
					--取消场次复式票，如果奖金大于1w，要先算单式票奖金是否大于1w，如果大于1w扣税
					if SpInput[i] == 1 then
						cancelNum = cancelNum * vSelect[i]
					end
				end

				tmpresult = tmpresult * 2

				--取消场次复式票，如果奖金大于1w，要先算单式票奖金是否大于1w，如果大于1w扣税
				if (tmpresult / cancelNum) > 10000 then
					tmpresult = tmpresult * 0.8
				end


				tmpresult = SiSheLiuRuWuChengShuang(tmpresult, 1)
				--tmpresult = math.floor(tmpresult*100+0.5)/100
				if tmpresult > 200000 and #vSelect + 1 >= 2 and #vSelect + 1 <= 3 then
					result = result + 200000
				elseif tmpresult > 500000 and #vSelect + 1 >= 4 and #vSelect + 1 >= 5 then
					result = result + 500000
				elseif tmpresult > 1000000 and #vSelect + 1 >= 6 then
					result = result + 1000000
				else
					result = result + tmpresult
				end
			end
        elseif nTotal == nSelect then
               tmpresult = 1

                for i = 1, nTotal do
					if pnInput[i] == -1 then
						tempInput[i] = 0
					elseif pnInput[i] == 0 then
						tempInput[i] = 1
					end
					tmpresult = tmpresult * tempInput[i]
					if SpInput[i] == 1 then
						cancelNum = cancelNum * pnInput[i]
					end
                end

				for i = 1, #vSelect do
					tmpresult = tmpresult * vSelect[i]
					--取出场次为取消的场次
					if vSpSelect[i] == 1 then
						cancelNum = cancelNum * vSelect[i]
					end
				end
				tmpresult = tmpresult * 2

				--取消场次复式票，如果奖金大于1w，要先算单式票奖金是否大于1w，如果大于1w扣税
				if (tmpresult / cancelNum) > 10000 then
					tmpresult = tmpresult * 0.8
				end

				--tmpresult = math.floor(tmpresult*100+0.5)/100
				tmpresult = SiSheLiuRuWuChengShuang(tmpresult, 1)
				if tmpresult > 200000 and nSelect + #vSelect >= 2 and nSelect + #vSelect <= 3 then
					result = result + 200000
				elseif tmpresult > 500000 and nSelect + #vSelect >= 4 and nSelect + #vSelect >= 5 then
					result = result + 500000
				elseif tmpresult > 1000000 and vSelect + #vSelect >= 6 then
					result = result + 1000000
				else
					result = result + tmpresult
				end

        elseif nTotal > nSelect then
                for i = 2, #pnInput do
                        temp[i - 1] = pnInput[i]
                end
				if pnInput[1] == 0 then
					tempInput[1] = 1
				elseif pnInput[1] == -1 then
					tempInput[1] = 0
				end
				k = #vSelect

				for i = 1, #vSelect do
					vTmpSelect[i] = vSelect[i]
				end
				vTmpSelect[#vSelect + 1] = tempInput[1]

				for i = 2, #SpInput do
                        tempSpInput[i - 1] = SpInput[i]
                end

				for i = 1, #vSpSelect do
					vTempSpSelect[i] = vSpSelect[i]
				end
				vTempSpSelect[#vSpSelect + 1] = SpInput[1]


                result =ComputeJCZQMoney(temp,nTotal - 1, nSelect - 1, vTmpSelect, tempSpInput, vTempSpSelect) + ComputeJCZQMoney(temp,nTotal - 1, nSelect,vSelect, tempSpInput, vSpSelect)


        end


        return result
end


function check_jc(playId, butType, inStr)
	local butNum = 0
	local butPower
	local butStr = ""
	local itemList = {}
	local dhItem = {}  		--逗号,
	local sxItem = {}   	--竖线|
	local jhItem = {}		--右尖括号>
	local xxItem = {}  		-- 斜线/
	local equalItem = {} 	--等号 =
	local nInput = {}
	local nTotal = 0
	local m					--过关组合m*n
	local n
	local pos = 0
	local flag = 0
	local fhItem = {}		--分号
	local cancelItem = {}
	local cancelInput = {}
	local cancelButNum = 0
	local oriStr = {}



	if string.find(inStr, "null") ~= nil or string.find(inStr, "=,") ~= nil then return 0 end

	fhItem = Split(inStr, ";")
	itemList = Split(fhItem[1], "#B" )
	if fhItem[2] ~= nil then
		cancelItem = Split(fhItem[2], ",")
	end
	itemLen = table.getn( itemList )

	if tonumber( itemList[2] ) then butPower = tonumber( itemList[2] ) else butPower = 1 end
	if butPower > MaxPower or butPower < 1 then return 0 end

	sxItem = Split(itemList[1],"|")
	dhItem = Split(sxItem[1],",")
	nTotal = table.getn(dhItem)

	---
	if butType == '1' or butType == '2' or butType == '3' or butType == '4' or butType == '5' or butType == '6' then
		k = 1
		for i = 1, #dhItem do
			equalItem = Split(dhItem[i], "=")
			if #equalItem ~= 2 then return 0 end
			xxItem = Split(equalItem[2],"/")
			nInput[i] = table.getn(xxItem)
			if butType == '1'  and (#xxItem > 3 or #xxItem < 1) then return 0 end
			if butType == '6' and (#xxItem > 3 or #xxItem < 1) then return 0 end
			if butType == '2' and (#xxItem > 8 or #xxItem < 1) then return 0 end
			if butType == '3' and (#xxItem > 4 or #xxItem < 1) then return 0 end
			if butType == '4' and (#xxItem > 31 or #xxItem < 1) then return 0 end
			if butType == '5' and (#xxItem > 9 or #xxItem < 1) then return 0 end
			for j = 1,#xxItem do
				if not just_str(playId, butType,xxItem[j]) then return 0 end
			end
			for j = 1,#cancelItem do
				if equalItem[1] == cancelItem[j] then
					cancelInput[k] = nInput[i]
					k = k + 1
				end
			end
		end
		if sxItem[2] == '1*1' then
			singleStr = Split(sxItem[1],",")
			for i  = 1, #singleStr do
				if playId == 'p' then
					butStr = butStr..JCBHMapping[tonumber(butType)]..singleStr[i]..'|'..sxItem[2]
				else
					butStr = butStr..BJDCBHMapping[tonumber(butType)]..singleStr[i]..'|'..sxItem[2]
				end
				if i < #singleStr then
					butStr = butStr ..';'
				end
			end
		else
			if playId == 'p' then
				butStr = JCBHMapping[tonumber(butType)]..itemList[1]
			else
				butStr = BJDCBHMapping[tonumber(butType)]..itemList[1]
			end
		end
	elseif butType == '7' then
		k = 1
		for i = 1, #dhItem do
			jhItem = Split(dhItem[i], ">")
			if jhItem[1] == "RQSPF" or jhItem[1] == "SPF" then
				butType = '1'
			elseif jhItem[1] == "JQS" then
				butType = '2'
			elseif jhItem[1] == "CBF" then
				butType = '4'
			elseif jhItem[1] == "BQC" then
				butType = '5'
			end
			equalItem = Split(jhItem[2], "=")
			if #equalItem ~= 2 then return 0 end
			xxItem = Split(equalItem[2],"/")
			nInput[i] = table.getn(xxItem)
			if butType == '1'  and (#xxItem > 3 or #xxItem < 1) then return 0 end
			if butType == '2' and (#xxItem > 8 or #xxItem < 1) then return 0 end
			if butType == '4' and (#xxItem > 31 or #xxItem < 1) then return 0 end
			if butType == '5' and (#xxItem > 9 or #xxItem < 1) then return 0 end
			for j = 1,#xxItem do
				if not just_str(playId, butType,xxItem[j]) then return 0 end
			end
			for j = 1,#cancelItem do
				if equalItem[1] == cancelItem[j] then
					cancelInput[k] = nInput[i]
					k = k + 1
				end
			end

		end
		butType = '7'
		if sxItem[2] == '1*1' then
			singleStr = Split(sxItem[1],",")
			for i  = 1, #singleStr do
				butStr = butStr..JCBHMapping[tonumber(butType)]..singleStr[i]..'|'..sxItem[2]
				if i < #singleStr then
					butStr = butStr ..';'
				end
			end
		else
			butStr = JCBHMapping[tonumber(butType)]..itemList[1]
		end

	end


	xItem = Split(sxItem[2], "*")
	m = tonumber(xItem[1])
	n = tonumber(xItem[2])

	if butType == '1' and playId == 'p' and m > 8 then return 0 end
	if butType == '6' and playId == 'p' and m > 8 then return 0 end
	if butType == '2' and playId == 'p' and m > 6 then return 0 end
	if butType == '4' and playId == 'p' and m > 4 then return 0 end
	if butType == '5' and playId == 'p' and m > 4 then return 0 end

	temp = 0
	pos = PosMapping[sxItem[2]]
	flag = 1
	if pos == nil then
		flag = 0
		for i = 1, m do
			temp = temp + combination(m, m + 1 - i)
			if temp == n then
				pos = m + 1 - i
				break
			end
		end
	end

	if(sxItem[2] == '6*35') then
		pos = 2
		m = 3
	elseif(sxItem[2] == '6*50') then
		pos = 2
		m = 4
	elseif(sxItem[2] == '5*20') then
		pos = 2
		m = 3
	end

	if flag == 0 then
		for i = pos, m do
			butNum = butNum + Compute(nInput, nTotal, i)
		end
		nTotal = #cancelInput
		if nTotal > m then
			nTotal = m
		end
		for i = pos, nTotal do
			cancelButNum = cancelButNum + Compute(cancelInput, #cancelInput, i)
		end
	elseif flag == 1 then
		butNum = butNum + Compute(nInput, nTotal, pos)
		if #cancelInput >=  pos then
			cancelButNum = cancelButNum + Compute(cancelInput, #cancelInput, pos)
		end
	end

	butNum = butNum - cancelButNum
	butStr = butStr..'#B'..butPower



	return butNum, butPower, butStr, butStr
end

function equalSplit(inStr)
	local dhItem = {}
	local tempItem = {}
	local outStr = {}
	local sp = {}
	local sxItem = {}

	dhItem = Split(inStr,",")
	for	i = 1,#dhItem do
		outStr[i] = {}
		tempItem = Split(dhItem[i],"=")
		outStr[i][1] = tempItem[1]

		sxItem = Split(tempItem[2],"|")
		if #sxItem == 2 then
			sp[i] = tonumber(sxItem[2])
			outStr[i][2] = sxItem[1]
		else
			outStr[i][2] = tempItem[2]
		end
	end
	return outStr, sp
end



function query_jc(playId, butType, inStr, bingoStr)

	local equalLotItem = {{}}
	local equalBingoItem = {{}}
	local dhItem = {}
	local tempItem = {}
	local pos = {}
	local sxItem = {}
	local jhItem = {}
	local Sp = {}
	local halfHost = {}
	local halfGuest = {}
	local wholeHost = {}
	local wholeGuest = {}
	local halfPoint = {}
	local wholePoint = {}
	local tempPoint = {}
	local concedePoint = {}
	local temp = {}
	local nInput = {}
	local bingoPos = {}
	local bingoGame = ""
	local totalAmount = 0
	local butPower = 0
	local fixedBonus = {}
	local variableBonus = {}
	local nCancelMatchNum = 0  --取消的场次个数
	local nTotalMoney = 0
	local jItem

	temp = Split(inStr,"#B")
	if tonumber( temp[2] ) then butPower = tonumber( temp[2] ) else butPower = 1 end

	sxItem = Split(temp[1],"|")


	equalLotItem = equalSplit(sxItem[2])

	jItem = Split(bingoStr, "#")
	equalBingoItem, Sp = equalSplit(jItem[1])
	nTotalMoney = tonumber(jItem[2])

	for i = 1, #equalBingoItem do
		if Sp[i] == 1 then
			halfHost[i] = 0
			halfGuest[i] = 0
			wholeHost[i] = 0
			wholeGuest[i] = 0
			concedePoint[i] = 0
		else
			tempPoint = Split(equalBingoItem[i][2], "*")
			temp = Split(tempPoint[1],":")
			halfHost[i] = tonumber(temp[1])
			halfGuest[i] = tonumber(temp[2])
			temp = Split(tempPoint[2], ":")
			wholeHost[i] = tonumber(temp[1])
			wholeGuest[i] = tonumber(temp[2])
			concedePoint[i] = tonumber(tempPoint[3])
		end
	end

	for i = 1,#equalLotItem do
		for j = 1, #equalBingoItem do
			if tonumber(equalLotItem[i][1]) == tonumber(equalBingoItem[j][1]) and playId == 'q'  then
				pos[i] = j
				break
			elseif string.find(equalLotItem[i][1], equalBingoItem[j][1]) ~= nil  and playId == 'p' then
				pos[i] = j
				break
			end
		end
	end



	--让球胜负平 胜负平
	if butType == '1' or butType == '6' then
		k = 1
		for i = 1, #equalLotItem do
			nTemp = wholeHost[pos[i]] + concedePoint[pos[i]] - wholeGuest[pos[i]]
			if  nTemp > 0 then
				bingo = 3
			elseif nTemp == 0 then
				bingo = 1
			else
				bingo = 0
			end
			if string.find(equalLotItem[i][2],bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end
		end
	--进球数
	elseif butType == '2' then
		k = 1
		for i = 1, #equalLotItem do
			bingo = wholeHost[pos[i]] + wholeGuest[pos[i]]
			if bingo > 7 then
				bingo = 7
			end
			if string.find(equalLotItem[i][2], bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end

		end
	--上下盘
	elseif butType == '3' then
		k = 1
		for i = 1, #equalLotItem do
			bingoSX = ""
			bingoDS = ""
			nTemp = wholeHost[pos[i]] + wholeGuest[pos[i]]
			if nTemp >= 3 then
				bingoSx = "S"
			else
				bingoSX = "X"
			end

			if nTemp % 2 == 0 then
				bingoDS = "S"
			else
				bingoDS = "D"
			end

			bingo = bingoSX ..bingoDS

			if string.find(equalLotItem[i][2], bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end

		end
	--猜比分
	elseif butType == '4' then
		k = 1
		for i = 1, #equalLotItem do
			bingo = wholeHost[pos[i]]..":"..wholeGuest[pos[i]]
			if not string.find("1:0/2:0/3:0/4:0/2:1/3:1/4:1/3:2/4:2/5:0/5:1/5:2/0:0/1:1/2:2/3:3/0:1/0:2/0:3/0:4/1:2/1:3/1:4/2:3/2:4/0:5/1:5/2:5", bingo) then
				if wholeHost[pos[i]] > wholeGuest[pos[i]] then
					bingo = '9:0'
				elseif wholeHost[pos[i]] == wholeGuest[pos[i]] then
					bingo = '9:9'
				else
					bingo = '0:9'
				end
			end
			if string.find(equalLotItem[i][2], bingo) ~= nil  or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
				k = k + 1
			else
				nInput[i] = -1
			end
		end
	--半全场
	elseif butType == '5' then

		k = 1
		for i = 1, #equalLotItem do
			bingoWhole = -1
			bingoHalf = -1
			nTemp = wholeHost[pos[i]] - wholeGuest[pos[i]]
			if  nTemp > 0 then
				bingoWhole = 3
			elseif nTemp == 0 then
				bingoWhole = 1
			else
				bingoWhole = 0
			end

			nTempHalf = halfHost[pos[i]] - halfGuest[pos[i]]
			if  nTempHalf > 0 then
				bingoHalf = 3
			elseif nTempHalf == 0 then
				bingoHalf = 1
			else
				bingoHalf = 0
			end

			bingo = bingoHalf.."x"..bingoWhole
			equalLotItem[i][2] = string.gsub(equalLotItem[i][2], "-", "x")
			if string.find(equalLotItem[i][2], bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
					k = k + 1
			else
				nInput[i] = -1
			end
		end
	--混合投注。
	elseif butType == '7' then
		k = 1
		for i = 1, #equalLotItem do
			jhItem = Split(equalLotItem[i][1], ">")
			if jhItem[1] == "RQSPF" or jhItem[1] == "SPF" then
				nTemp = wholeHost[pos[i]] + concedePoint[pos[i]] - wholeGuest[pos[i]]
				if  nTemp > 0 then
					bingo = 3
				elseif nTemp == 0 then
					bingo = 1
				else
					bingo = 0
				end
			elseif jhItem[1] == "JQS" then
				bingo = wholeHost[pos[i]] + wholeGuest[pos[i]]
				if bingo > 7 then
					bingo = 7
				end
			elseif jhItem[1] == "CBF" then
				bingo = wholeHost[pos[i]]..":"..wholeGuest[pos[i]]
				if not string.find("1:0/2:0/3:0/4:0/2:1/3:1/4:1/3:2/4:2/5:0/5:1/5:2/0:0/1:1/2:2/3:3/0:1/0:2/0:3/0:4/1:2/1:3/1:4/2:3/2:4/0:5/1:5/2:5", bingo) then
					if wholeHost[pos[i]] > wholeGuest[pos[i]] then
						bingo = '9:0'
					elseif wholeHost[pos[i]] == wholeGuest[pos[i]] then
						bingo = '9:9'
					else
						bingo = '0:9'
					end
				end
			elseif jhItem[1] == "BQC" then
				bingoWhole = -1
				bingoHalf = -1
				nTemp = wholeHost[pos[i]] - wholeGuest[pos[i]]
				if  nTemp > 0 then
					bingoWhole = 3
				elseif nTemp == 0 then
					bingoWhole = 1
				else
					bingoWhole = 0
				end

				nTempHalf = halfHost[pos[i]] - halfGuest[pos[i]]
				if  nTempHalf > 0 then
					bingoHalf = 3
				elseif nTempHalf == 0 then
					bingoHalf = 1
				else
					bingoHalf = 0
				end

				bingo = bingoHalf.."x"..bingoWhole
				equalLotItem[i][2] = string.gsub(equalLotItem[i][2], "-", "x")
			end


			if string.find(equalLotItem[i][2], bingo) ~= nil or Sp[i] == 1 then
				if Sp[i] == 1 then
					LotNum = Split(equalLotItem[i][2], "/")
					nInput[i] = #LotNum
					nCancelMatchNum = nCancelMatchNum + 1
				else
					nInput[i] = Sp[pos[i]]
				end
				bingoPos[k] = pos[i]
					k = k + 1
			else
				nInput[i] = -1
			end
		end
	end

	nTotal = #equalLotItem
	xItem = Split(sxItem[3], "*")
	m = tonumber(xItem[1])
	n = tonumber(xItem[2])
	temp = 0
	position = PosMapping[sxItem[3]]
	flag = 1
	if position  == nil then
		flag = 0
		for i = 1, m do
			temp = temp + combination(m, m + 1 - i)
			if temp == n then
				position  = m + 1 - i
				break
			end
		end
	end

	if(sxItem[3] == '6*35') then
		position = 2
		m = 3
	elseif(sxItem[3] == '6*50') then
		position = 2
		m = 4
	elseif(sxItem[3] == '5*20') then
		position = 2
		m = 3
	end

	if nCancelMatchNum == m then
		totalAmount = nTotalMoney
	elseif sxItem[3] == '1*1' and playId == 'p' then
		if butType == '1' or butType == '2' or butType == '5' or butType == '6' then
			for i = 1, #bingoPos do
				if nInput[bingoPos[i]] > 5000000 then
					totalAmount = totalAmount + 5000000*butPower
				elseif 2*nInput[bingoPos[i]] < 2 then
					totalAmount = totalAmount + 2*butPower
				else
					totalAmount = totalAmount + 2*nInput[bingoPos[i]] * butPower
				end
			end
		else
			for i = 1, #bingoPos do
				if 2*nInput[bingoPos[i]] < 2 then
					totalAmount = totalAmount + 2*butPower
				else
					totalAmount = totalAmount + 2*nInput[bingoPos[i]] * butPower
				end
			end
		end
	else
		if playId == 'q' then
			if flag == 0 then
				for i = position, m do
					nTmp = ComputeMoney(nInput, nTotal, i)
					if nTmp > 0 and nTmp < 2 then
						nTmp = 2
					end
					totalAmount = totalAmount + nTmp
				end
			elseif flag == 1 then
				nTmp = ComputeMoney(nInput, nTotal, position )
				if nTmp > 0 and nTmp < 2 then
					nTmp = 2
				end
				totalAmount = totalAmount + nTmp
			end
			totalAmount = totalAmount * butPower
		elseif playId == 'p' then
			if flag == 0 then
				for i = position, m do
					--print(ComputeJCZQMoney(nInput, nTotal, i, {}))
					totalAmount = totalAmount + ComputeJCZQMoney(nInput, nTotal, i, {} , Sp, {})
				end

			elseif flag == 1 then
				--print(ComputeJCZQMoney(nInput, nTotal, position, {}))
				totalAmount = totalAmount + ComputeJCZQMoney(nInput, nTotal, position, {}, Sp, {})

			end
			totalAmount = totalAmount * butPower
		end
	end
	--竞彩奖金计算，要单倍四舍五入再乘以倍数
	--totalAmount = math.floor((totalAmount/butPower)*100+0.5)/100*butPower

	for i = 1, #bingoPos do
		bingoGame = bingoGame..equalBingoItem[bingoPos[i]][1]
		if i < #bingoPos then bingoGame = bingoGame.."," end
	end




	return totalAmount , bingoGame

end


--print(query_jc('p', '7', "HH|SPF>150801001=1/3,RQSPF>150801002=0/1,SPF>150801003=0/3,SPF>150801004=1/3,RQSPF>150801005=1/3,RQSPF>150801006=1/3,SPF>150801007=0/1,RQSPF>150801008=0/1|8*1#B1","150801001=0:1*1:1*0|3.200,150801002=1:2*1:2*+1|3.400,150801003=0:0*2:0*0|2.520,150801004=0:0*0:0*0|1,150801005=0:0*2:0*-1|4.050,150801006=1:0*2:0*-1|3.600,150801007=0:1*1:1*0|3.100,150801008=1:1*1:2*+1|3.400#512"))

--print(check_jc('p', '1', "141008001=3,141008002=3,141008003=3,141008004=3,141008005=3|5*20"))
--print(check_jc('p', '4', "121119001=9:0/1:0/2:0,121119002=3:0/9:9/0:9/0:2,121119004=3:2/2:3/0:4|3*1#B1"))
--print(check_jc('q', '3', "1=SS,2=SS,3=SS,4=SS,5=SD,6=SD|5*10"))
--print(check_jc('q', '1', "44=1|1*1"))
--print(jc_check('q', '4', "48=4:0,49=4:0|1*1"))
--print(jc_check('q', '5', "1=1-3,2=1-0,3=1-0,4=1-0,5=1-0|1*1"))
--print(jc_query('p', '5', "1=1-3,2=3-1,3=1-0,4=1-0,5=1-0|1*1", "1=1:1*4:3*10*11|2,2=1:1*3:4*0*10*11|2,3=1:1*3:4*0*10*11|2,4=1:1*3:4*0*10*11|2,5=1:1*3:4*0*10*11|2,6=1:1*3:4*0*10*11|2,7=1:1*3:4*0*10*11|2"))
--print(jc_query('p', '5', "1=1-0,2=1-0,3=1-0,4=1-0,5=0-0,6=0-0|4*11", "1=1:1*3:4*0|1.3,2=1:1*3:4*0|2.65,3=1:1*3:4*0|4.1,4=1:1*3:4*0|3.7,5=1:1*3:4*0|1.85,6=1:1*3:4*0|3.3,7=1:1*3:4*0|2"))
--print(jc_query('q', '5', "1=1-0,2=1-0,3=1-0,4=1-0,5=0-0|1*1", "1=1:1*3:4*0|2.25,2=1:1*3:4*0|2.99,3=1:1*3:4*0|4.15,4=1:1*3:4*0|7.6,5=1:1*3:4*0|1.85,6=1:1*3:4*0|3.3,7=1:1*3:4*0|2"))

--竞彩足球兑奖测试
--print(query_jc('p', '7', "HH|RQSPF>141007002=0,RQSPF>141007009=0/3,RQSPF>141007011=0/3,SPF>141007013=3|4*1#B2","141007002=0:2*0:3*-1|1.490,141007009=2:0*3:1*-1|2.800,141007011=2:1*2:3*-1|2.660,141007013=1:0*3:2*0|1.490#16"))
--print(query_jc('q', '1', "SPF|56=3,156=3,158=3/1|3*1#B1","56=1:0*2:0*-1|2.42,156=1:1*1:1*-1|4.71,158=1:0*3:0*-2|2.74#4 "))
--print(query_jc('p', '7', "HH|RQSPF>130922002=3,SPF>130922001=3|2*1#B1", "130922002=1:0*2:1*+1|1.510,130922001=1:0*1:0*0|2.400#2"))
--print(jc_query('p', '2', "1=7,2=7,3=7,4=7,5=6|3*4", "1=1:1*3:4*0|1.78,2=1:1*3:4*0|2.75,3=1:1*3:4*0|3.55,4=1:1*3:4*0|5.0,5=1:1*3:4*0|3.2,6=1:1*3:4*0|4.0,7=1:1*3:4*0|3.45,8=1:1*3:4*0|3.7,9=1:1*3:4*0|2.78,10=1:1*3:4*0|4.35"))
--print(query_jc('p', '1', "1=0,2=0,3=0,4=0|4*1", "1=1:1*3:4*0|2,2=1:1*3:4*0|2,3=1:1*3:4*0|2,4=1:1*3:4*0|2,5=1:1*3:4*0|3.2,6=1:1*3:4*0|4.0,7=1:1*3:4*0|3.45,8=1:1*3:4*0|3.7,9=1:1*3:4*0|2.78,10=1:1*3:4*0|4.35"))
--print(query_jc('q', '5', "BQC|152=3-3|1*1#B2", "152=1:0*3:0*-3|1.40#4"))
--print(query_jc('p', '7', "HH|SPF>1=0,RQSPF>2=1,BQC>3=1-0|3*1#B1", "1=1:1*3:4*0|2,2=1:1*3:4*1|2,3=1:1*3:4*0|2"))
--print(query_jc('p', '6', "SPF|150606015=0,150606016=1|2*1#B1", "150606015=0:1*0:1*0|1.5725,150606016=0:1*1:1*0|1"))
--print(query_jc('p', '6', "SPF|150606015=0,150606016=1|2*1#B1", "150606015=0:1*0:1*0|1.57745,150606016=0:1*1:1*0|1"))
--print(query_jc('p', '6', "SPF|150606015=0,150606016=1|2*1#B1", "150606015=0:1*0:1*0|1.5775,150606016=0:1*1:1*0|1"))
--print(query_jc('p', '6', "SPF|150606015=0,150606016=1|2*1#B1", "150606015=0:1*0:1*0|1.57755,150606016=0:1*1:1*0|1"))
--print(query_jc('p', '6', "SPF|150606015=0,150606016=1|2*1#B1", "150606015=0:1*0:1*0|1.578,150606016=0:1*1:1*0|1"))
--print(query_jc('p', '6', "SPF|150606015=0,150606016=1|2*1#B1", "150606015=0:1*0:1*0|1.2,150606016=0:1*1:1*0|1"))
--北京单场中奖测试
--print(query_jc('q', '1', "SPF|3=0,12=0,2=0,26=0|4*1#B1","3=0:1*0:4*+2|5.87,12=0:0*1:0*-2|2.06,2=1:0*1:0*-2|1.85,26=1:0*4:0*-2|3.02#2"))
--print(query_jc('q', '2', "SPF|1=7,2=7,3=7,4=7,5=7,6=7,7=7,8=7,9=7|6*63", "1=1:1*3:4*0|2.22,2=1:1*3:4*0|2.54,3=1:1*3:4*0|3.57,4=1:1*3:4*0|7.51,5=1:1*3:4*0|4.44,6=1:1*3:4*0|5.23,7=1:1*3:4*0|2.41,8=1:1*3:4*0|2.23,9=1:1*3:4*0|3.03,10=1:1*3:4*0|4.35#0"))
--print(jc_query('q', '3', "1=SD,2=SD,3=SD,4=SD,5=SD,6=SD,7=SD,8=SD,9=SD|6*63", "1=1:1*3:4*0|2.22,2=1:1*3:4*0|2.54,3=1:1*3:4*0|3.57,4=1:1*3:4*0|7.51,5=1:1*3:4*0|4.44,6=1:1*3:4*0|5.23,7=1:1*3:4*0|2.41,8=1:1*3:4*0|2.23,9=1:1*3:4*0|3.03,10=1:1*3:4*0|4.35"))
--print(jc_query('q', '4', "1=3:4,2=3:4,3=3:4,4=3:4,5=3:4,6=3:4,7=3:4,8=3:4,9=3:4|6*63", "1=1:1*3:4*0|2.22,2=1:1*3:4*0|2.54,3=1:1*3:4*0|3.57,4=1:1*3:4*0|7.51,5=1:1*3:4*0|4.44,6=1:1*3:4*0|5.23,7=1:1*3:4*0|2.41,8=1:1*3:4*0|2.23,9=1:1*3:4*0|3.03,10=1:1*3:4*0|4.35"))
--print(jc_query('q', '5', "1=1-0,2=1-0,3=1-0,4=1-0,5=1-0,6=1-0,7=1-0,8=1-0,9=1-0|6*63", "1=1:1*3:4*0|2.22,2=1:1*3:4*0|2.54,3=1:1*3:4*0|3.57,4=1:1*3:4*0|7.51,5=1:1*3:4*0|4.44,6=1:1*3:4*0|5.23,7=1:1*3:4*0|2.41,8=1:1*3:4*0|2.23,9=1:1*3:4*0|3.03,10=1:1*3:4*0|4.35"))
--print(Compute({3,1,1,1,1,1,2},7,4))
--print(SiSheLiuRuWuChengShuang(677.286, 90))
--print(query_jc('p','6',"SPF|150810006=1,150810010=0|2*1#B96", "150810006=0:0*1:1*0|3.400,150810010=0:2*0:3*0|1.500"))
